import java.util.*
// import kotlin.collections.toIntArray
import kotlin.collections.toTypedArray

class Solution {
    fun findAllConcatenatedWordsInADict(words: Array<String>): List<String> {
        var infos = HashMap<Int,HashSet<String>>()
        words.forEach{
            var currentLen = it.length
            if(currentLen > 0){
                if(!infos.contains(currentLen)){
                    infos.put(currentLen, HashSet<String>())
                }
                infos[currentLen]!!.add(it)
            }
        }
        var lens = infos.keys.sorted()

        var ans = ArrayList<String>()
        for(word in words){
            if(isExist(infos, lens, word) > 1){
                ans.add(word)
            }
        }
        return ans
    }
    var memory = HashMap<String, Int>()
    fun isExist(infos:HashMap<Int,HashSet<String>>,lens:List<Int>, word:String):Int{
        var min = -600003
        var ans = min
        if(word.length == 0){
            return 0
        }
        if(memory.contains(word)){
            return memory[word]!!
        }
        for(len in lens){
            if(len > word.length){
                break
            }
            infos[len]?.let{
                if(it.contains(word.slice(0 until len))){
                    ans = Math.max(ans, isExist(infos, lens, word.substring(len)) + 1)
                }
            }
        }
        memory.put(word, ans)
        return ans
    }
}

fun main(args: Array<String>) {
    val scanner = Scanner(System.`in`)
    // words: Array<String>
    val wordInput = scanner.nextLine()
    val wordMap = wordInput?.removeSurrounding(
        "[",
        "]"
    )?.split(",")?.map { it.trim('"') }
    // it.toString()
    // println(wordMap)
    if (wordMap != null) {
        val word = wordMap.toTypedArray()
        for (w in word) {
            println(w)
        }
        println(word.contentToString())
        // val adapter = ChatAdapter(ArrayList(wordMap))
        // val adapter = arrayOf(wordMap)
        // println(adapter)
        // 输出空的？  []
         println(Solution().findAllConcatenatedWordsInADict(word))
        // findAllConcatenatedWordsInADict(words: Array<String>): List<String>
        // arrayListOf<>
        val stringArray :Array<String> = arrayOf("cat","dog","catdog")
        val result: List<String> = Solution().findAllConcatenatedWordsInADict(stringArray)
        println(result)
    }
}